using System;
using System.Collections;
using System.Collections.Generic;
using BitMango.Diagnostics;
using Data;
using TMPro;
using UnityEngine;

public class PackageSceneHandler : MonoBehaviour, IGameScene, IScrollLevel, IHandlerBase, IScrollPage
{
	public void DoDataExchange()
	{
	}

	public void OnEnterScene(object param)
	{
		if (Scene.IsPopupOpen("SettingPopup"))
		{
			Scene.ClosePopup("SettingPopup", null);
		}
		this.clearCurtain.SetActive(false);
		foreach (PackageWidgetHandler packageWidgetHandler in this.widgets)
		{
			UnityEngine.Object.Destroy(packageWidgetHandler.gameObject);
		}
		this.widgets.Clear();
		this.top.transform.localPosition = new Vector3(this.top.transform.localPosition.x, BlockBannerAnchor.GetTopPositionY(), this.top.transform.localPosition.z);
		float bannerPositionY = BlockBannerAnchor.GetBannerPositionY();
		this.bottom.transform.localPosition = new Vector3(this.bottom.transform.localPosition.x, bannerPositionY, this.bottom.transform.localPosition.z);
		this.SetClearCount();
		this.CreatePackageList();
		this.CreatePremiumPackageList();
		this.title.text = "Package";
		//if (Director.isShowReviewPopup && !DataContainer<Data.Root>.Data.userdata.record.isReviewed)
		//{
		//	base.StartCoroutine(this.coHavingFunPopup());
		//}
		if (this.pageX.PageIndex == 0)
		{
			this.btnTab.SetSprite("collection_tab_classic");
		}
		else if (this.pageX.PageIndex == 1)
		{
			this.btnTab.SetSprite("collection_tab_premium");
		}
		this.SetArrow();
		this.finger.SetActive(false);
		if (DataContainer<Data.Root>.Data.tutorialdata.record.isFirstPremium)
		{
			this.finger.SetActive(true);
		}
	}

	public void OnLeaveScene()
	{
	}

	private void SetArrow()
	{
		this.arrowRight.SetActive(false);
		this.arrowLeft.SetActive(false);
		/*if (this.pageX.PageIndex == 0 && this.pageX.PageIndex == this.pageX.PageCount - 1)
		{
			this.arrowRight.SetActive(false);
			this.arrowLeft.SetActive(false);
		}
		else if (this.pageX.PageIndex == 0)
		{
			this.arrowRight.SetActive(true);
			this.arrowLeft.SetActive(false);
		}
		else if (this.pageX.PageIndex == this.pageX.PageCount - 1)
		{
			this.arrowRight.SetActive(false);
			this.arrowLeft.SetActive(true);
		}
		else
		{
			this.arrowRight.SetActive(true);
			this.arrowLeft.SetActive(true);
		}*/
	}

	public void OnChangedPage(tk2dUIScrollPage scrollPage, GameObject page, int pageIndex)
	{
		Debugger.Assert(scrollPage != null, "PackageSceneHandler OnChangedPage");
		Debugger.Assert(page != null, "PackageSceneHandler OnChangedPage");
		if (pageIndex == 0)
		{
			DWATTHelper.logEvent("ChangePage", new object[]
			{
				"Classic"
			});
		}
		else if (pageIndex == 1)
		{
			DWATTHelper.logEvent("ChangePage", new object[]
			{
				"Premium"
			});
		}
		if (pageIndex == 0)
		{
			this.btnTab.SetSprite("collection_tab_classic");
		}
		else if (pageIndex == 1)
		{
			//this.btnTab.SetSprite("collection_tab_premium");
			//if (DataContainer<Data.Root>.Data.tutorialdata.record.isFirstPremium)
			//{
			//	DataContainer<Data.Root>.Data.tutorialdata.record.isFirstPremium = false;
			//	DataContainer<Data.Root>.Data.tutorialdata.SaveContext();
			//	this.finger.SetActive(false);
			//	Scene.QueuePopup("PremiumTutorialPopup", null, delegate(object isOK)
			//	{
			//		this.pageX.GoToPrevPage();
			//	});
			//}

            this.pageX.GoToNextPage();
        }
		this.currentPage = pageIndex;
		this.SetArrow();
		this.ScrollYCallBack();
	}

	public void OnUpdatePage(tk2dUIScrollPage scrollPage, GameObject page, int pageIndex)
	{
		Debugger.Assert(scrollPage != null, "PackageSceneHandler OnUpdatePage");
		Debugger.Assert(page != null, "PackageSceneHandler OnUpdatePage");
		if (this.pageArea[pageIndex].Value <= 0f)
		{
			this.pageArea[pageIndex].Value = 0f;
		}
		else if (this.pageArea[pageIndex].Value >= 1f)
		{
			this.pageArea[pageIndex].Value = 1f;
		}
	}

	public void OnUpdateLevel(tk2dUIScrollPage scrollPage, GameObject level, int pageIndex, int levelIndex, out bool enable)
	{
		Debugger.Assert(scrollPage != null, "PackageSceneHandler OnUpdateLevel");
		Debugger.Assert(level != null, "PackageSceneHandler OnUpdateLevel");
		enable = false;
	}

	private IEnumerator coHavingFunPopup()
	{
		this.clearCurtain.SetActive(true);
		yield return new WaitForSeconds(0.5f);
		this.clearCurtain.SetActive(false);
		Director.isShowReviewPopup = false;
		DataContainer<Data.Root>.Data.userdata.record.isReviewed = true;
		DataContainer<Data.Root>.Data.userdata.SaveContext();
		//Scene.QueuePopup("HavingFunPopup", null, delegate(object rateType)
		//{
		//	if ((RateType)rateType == RateType.LOVE)
		//	{
		//		Scene.QueuePopup("RatePopup", null, delegate(object close)
		//		{
		//		});
		//	}
		//	else if ((RateType)rateType == RateType.NOTREALLY)
		//	{
		//		Scene.QueuePopup("FeedbackPopup", null, delegate(object close)
		//		{
		//		});
		//	}
		//});
		yield break;
	}

	private bool CreatePackageList()
	{
		Vector3 zero = Vector3.zero;
		Vector3 vector = Scene.CurrentScene().transform.InverseTransformPoint(this.topDown.transform.position);
		Vector3 vector2 = Scene.CurrentScene().transform.InverseTransformPoint(this.bottomUp.transform.position);
		zero.y = vector.y;
		zero.z = -1f;
		zero.y -= 4f;
		List<PackageWidgetHandler> list = new List<PackageWidgetHandler>();
		for (int i = 0; i < DataContainer<Data.Root>.Data.packages.Length; i++)
		{
			if (DataContainer<Data.Root>.Data.packages[i].mode.Equals(GameMode.packageMode))
			{
				if (!DataContainer<Data.Root>.Data.packages[i].isPremium)
				{
					PackageWidgetHandler packageWidgetHandler = Widget.Create<PackageWidgetHandler>();
					packageWidgetHandler.transform.parent = this.pageContents[0].transform;
					packageWidgetHandler.transform.localPosition = zero;
					zero.y -= this.widgetInterval;
					packageWidgetHandler.transform.localScale = Vector3.one;
					packageWidgetHandler.gameObject.name = string.Format("{0:D3}.{1}", i, packageWidgetHandler.gameObject.name);
					packageWidgetHandler.SetData(DataContainer<Data.Root>.Data.packages[i]);
					this.widgets.Add(packageWidgetHandler);
					list.Add(packageWidgetHandler);
				}
			}
		}
		GameObject gameObject = this.pageContents[0].gameObject;
		Bounds bounds = new Bounds(gameObject.transform.position, Vector3.zero);
		for (int j = 0; j < list.Count; j++)
		{
			bounds.Encapsulate(list[j].GetComponent<Renderer>().bounds);
		}
		this.pageArea[0].ContentLength = bounds.size.y + 3.5f;
		this.pageArea[0].VisibleAreaLength = Mathf.Abs(vector.y) + Mathf.Abs(vector2.y);
		return true;
	}

	private bool CreatePremiumPackageList()
	{
		Vector3 zero = Vector3.zero;
		Vector3 vector = Scene.CurrentScene().transform.InverseTransformPoint(this.topDown.transform.position);
		Vector3 vector2 = Scene.CurrentScene().transform.InverseTransformPoint(this.bottomUp.transform.position);
		zero.y = vector.y;
		zero.z = -1f;
		zero.y -= 4f;
		List<PackageWidgetHandler> list = new List<PackageWidgetHandler>();
		for (int i = 0; i < DataContainer<Data.Root>.Data.packages.Length; i++)
		{
			if (DataContainer<Data.Root>.Data.packages[i].mode.Equals(GameMode.packageMode))
			{
				if (DataContainer<Data.Root>.Data.packages[i].isPremium)
				{
					PackageWidgetHandler packageWidgetHandler = Widget.Create<PackageWidgetHandler>();
					packageWidgetHandler.transform.parent = this.pageContents[1].transform;
					packageWidgetHandler.transform.localPosition = zero;
					zero.y -= this.widgetInterval;
					packageWidgetHandler.transform.localScale = Vector3.one;
					packageWidgetHandler.gameObject.name = string.Format("{0:D3}.{1}", i, packageWidgetHandler.gameObject.name);
					packageWidgetHandler.SetData(DataContainer<Data.Root>.Data.packages[i]);
					this.widgets.Add(packageWidgetHandler);
					list.Add(packageWidgetHandler);
				}
			}
		}
		GameObject gameObject = this.pageContents[1].gameObject;
		Bounds bounds = new Bounds(gameObject.transform.position, Vector3.zero);
		for (int j = 0; j < list.Count; j++)
		{
			bounds.Encapsulate(list[j].GetComponent<Renderer>().bounds);
		}
		this.pageArea[1].ContentLength = bounds.size.y + 3.5f;
		this.pageArea[1].VisibleAreaLength = Mathf.Abs(vector.y) + Mathf.Abs(vector2.y);
		return true;
	}

	private void SetClearCount()
	{
		this.clearCount.text = string.Format("{0}/{1}", DataContainer<Data.Root>.Data.PackageClearCount(GameMode.packageMode), DataContainer<Data.Root>.Data.ModePackageCount(GameMode.packageMode));
	}

	private void OnClick_Form_back()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		Scene.ChangeTo("IntroScene", null);
	}

	private void OnClickRight()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
        // this.pageX.GoToNextPage();
 
        this.pageX.ChangePage(1, true, 0.5f);
    }

	private void OnClickLeft()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		// this.pageX.GoToPrevPage();
		this.pageX.ChangePage(0, true, 0.5f);

    }

	public void OnClick_package(PackageWidgetHandler package)
	{
		Debugger.Assert(package != null && package.data != null, "PackageSceneHandler OnClick_package");
		BMSoundHelper.Play("se_btnclick", false, 1f);
		DataContainer<Data.Root>.Data.currentPackage = ((!(package == null)) ? package.data : DataContainer<Data.Root>.Data.packages[0]);
		this.SyncPackage();
		if (DataContainer<Data.Root>.Data.currentPackage.record.isLocked)
		{
			if (DataContainer<Data.Root>.Data.currentPackage.iap != null)
			{
				Scene.QueuePopup("IapUnLockPackagePopup", false, delegate(object message)
				{
					if (message.ToString().Equals("free"))
					{
						Scene.ChangeTo("LevelScene", null);
					}
					package.SetData(DataContainer<Data.Root>.Data.currentPackage);
				});
			}
		}
		else
		{
			Scene.ChangeTo("LevelScene", null);
		}
	}

	private void OnClickCollection()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		DWATTHelper.logEvent("Collection", new object[]
		{
			"Enter"
		});
		Scene.ChangeTo("CollectionThemeScene", "PackageScene");
	}

	private void SyncPackage()
	{
		if (!DataContainer<Data.Root>.Data.currentPackage.record.isLocked)
		{
			for (int i = 1; i < DataContainer<Data.Root>.Data.currentPackage.levels.Length; i++)
			{
				if (DataContainer<Data.Root>.Data.currentPackage.levels[i].record.locked && DataContainer<Data.Root>.Data.currentPackage.levels[i - 1].record.isClear)
				{
					DataContainer<Data.Root>.Data.currentPackage.levels[i].record.locked = false;
					DataContainer<Data.Root>.Data.currentPackage.levels[i].SaveContext();
					break;
				}
			}
		}
	}

	private void OnEscape()
	{
		Scene.ChangeTo("IntroScene", null);
	}

	private void ScrollYCallBack()
	{
		if (this.pageArea[this.currentPage].Value >= 0.025f)
		{
			if (this.pageArea[this.currentPage].Value > 0.975f)
			{
			}
		}
		if (this.pageArea[this.currentPage].ContentLength < this.pageArea[this.currentPage].VisibleAreaLength)
		{
		}
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public tk2dUIScrollPage pageX;

	public GameObject[] pageContents;

	public tk2dUIScrollableArea[] pageArea;

	public GameObject top;

	public GameObject topDown;

	public GameObject bottom;

	public GameObject bottomUp;

	public GameObject clearCurtain;

	public TextMeshPro clearCount;

	public TextMeshPro title;

	public tk2dSprite btnTab;

	public GameObject arrowRight;

	public GameObject arrowLeft;

	public GameObject finger;

	private int currentPage;

	private List<PackageWidgetHandler> widgets = new List<PackageWidgetHandler>();

	private float originalwidgetInterval = 4f;

	private float widgetInterval = 4f;

	private List<ScrollRecord> scrollRecord = new List<ScrollRecord>();
}
